package com.googlecode.flealancemarket.tapestry.pages.inbox;

import com.googlecode.flealancemarket.core.tapestry.SpringBean;
import com.googlecode.flealancemarket.metamodel.PrivateMessage;
import com.googlecode.flealancemarket.metamodel.User;
import com.googlecode.flealancemarket.service.commands.GetUserByUsername;
import com.googlecode.flealancemarket.service.commands.SendPrivateMessage;
import com.googlecode.flealancemarket.tapestry.util.Session;
import java.util.Calendar;
import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.ioc.annotations.Inject;

/**
 *
 * @author vanja
 */
public class New extends AbstractInboxPage {

    @Persist
    private String username;
    @Persist
    private String title;
    @Persist
    private String message;

    @Inject
    @SpringBean("com.googlecode.flealancemarket.service.commands.GetUserByUsername")
    private GetUserByUsername getUser;

    @Inject
    @SpringBean("com.googlecode.flealancemarket.service.commands.SendPrivateMessage")
    private SendPrivateMessage sendPrivateMessage;

    @Inject
    @SpringBean("com.googlecode.flealancemarket.metamodel.PrivateMessage")
    private PrivateMessage privateMessage;

    @ApplicationState
    private Session session;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    Object onSubmitFromPrivateMessageForm(){
        getUser.setUsername(session.getUsername());
        User sender = getUser.execute();

        getUser.setUsername(getUsername());
        User receiver = getUser.execute();
        if(receiver==null) {
            session.setMessage(String.format("There's no user called %s.", getUsername()));
            return null;
        } else if(sender.equals(receiver)) {
            session.setMessage("You can't send message to yourself");
            return null;
        }
            session.setMessage("Kewl, " + sender.getUsername());
        privateMessage.setContent(getMessage());
        privateMessage.setRead(false);
        privateMessage.setReceiver(receiver);
        privateMessage.setSender(sender);
        privateMessage.setSendTime(Calendar.getInstance());
        privateMessage.setTitle(getTitle());
        
        sendPrivateMessage.setMessage(privateMessage);
        PrivateMessage response = sendPrivateMessage.execute();
        if(response==null){
            session.setMessage("Something went wrong. Try again later.");
            return null;
        }
        session.setMessage("Message sent");
        username = "";
        title = "";
        message = "";
        return Index.class;
    }

}
